package com.lzh.persevere.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.common.BaseEntity;
import com.lzh.persevere.mapper.FileInfoMapper;
import com.lzh.persevere.mapper.FriendCycleMapper;
import com.lzh.persevere.model.entity.FileInfo;
import com.lzh.persevere.model.entity.FriendCycle;
import com.lzh.persevere.model.qo.friendCycle.FindFriendCyclePageQO;
import com.lzh.persevere.service.FriendCycleService;
import com.lzh.persevere.template.PageAbstractTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 朋友圈表服务实现类
 *
 * @author luzhiheng
 * @since 2025-07-07
 */
@Service
public class FriendCycleServiceImpl extends ServiceImpl<FriendCycleMapper, FriendCycle> implements FriendCycleService {

    @Resource
    private RedisCacheService redisCacheService;

    @Resource
    private FileInfoMapper fileInfoMapper;

    @Override
    public Map<String, Object> pageList(FindFriendCyclePageQO qo) {
        Map<String, Object> otherQueryMap = Collections.emptyMap();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(qo.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pagedList = baseMapper.pageList(this.queryMap);

                Map<String, String> allUserIdAvatarMap = redisCacheService.getAllUserIdAvatarMap();
                Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();

                List<Long> fileIdList = new ArrayList<>();
                for (int i = 0; i < pagedList.size(); i++) {
                    Map<String, Object> map = pagedList.get(i);
                    map.put("position", i % 2 == 0 ? "left" : "right");

                    map.put("createNickName", allUserIdNickNameMap.get(String.valueOf(map.get("createUserId"))));
                    map.put("avatar", allUserIdAvatarMap.get(String.valueOf(map.get("createUserId"))));

                    List<Long> ids = new ArrayList<>();
                    Object obj = map.get("fileIds");
                    if (obj != null) {
                        for (String str : obj.toString().split(",")) {
                            if (StrUtil.isNotEmpty(str)) {
                                ids.add(Long.valueOf(str));
                            }
                        }
                    }
                    fileIdList.addAll(ids);
                    map.put("fileIds", ids);
                }
                Map<Long, String> fileIdMap;
                if (CollectionUtil.isNotEmpty(fileIdList)) {
                    fileIdMap = fileInfoMapper.selectByIdList(fileIdList).stream().collect(Collectors.toMap(BaseEntity::getId, FileInfo::getFileAddress));
                } else {
                    fileIdMap = new HashMap<>();
                }
                for (Map<String, Object> map : pagedList) {
                    List<Long> idList = (List<Long>) map.get("fileIds");
                    if (CollectionUtil.isNotEmpty(idList)) {
                        List<Map<String, Object>> imgList = idList.stream().map(e -> {
                            Map<String, Object> imgMap = new HashMap<>();
                            String imgUrl = fileIdMap.get(e);
                            imgMap.put("url", imgUrl);
                            imgMap.put("fileId", e);
                            return imgMap;
                        }).collect(Collectors.toList());
                        map.put("imgList", imgList);
                    }
                }
                return pagedList;
            }
        };
        return template.queryPage();
    }
}
